<?
/**
 * This file is part of XNAT light.
 *
 * XNAT light is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * XNAT light is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XNAT light.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace NRG\Framework\Views;

use NRG\Framework\Exceptions\IllegalArgumentException as IllegalArgumentException;
use NRG\Framework\Exceptions\ViewException as ViewException;
use NRG\Framework\Common\Registry as Registry;

/**
 * View class
 *
 * @package NRG\Framework\Views
 */
class View
{
    /**
     * Script name
     *
     * @var string
     */
    protected $_script;
 
    /**
     * Base URL
     *
     * @var string
     */
    protected $_base;

    /**
     * Constructor
     *
     * @param string $controller
     * @param string $action
     */
    public function __construct($controller=null, $action=null)
    {
	if($controller == null)
	    throw new IllegalArgumentException("View controller " .
		    "cannot be null");

	if($action == null)
	    throw new IllegalArgumentException("View action " .
		    "cannot be null");

	$this->_script = "$controller/$action.php";

	if(!file_exists(dirname(__FILE__) . "/$this->_script"))
	{
	    $index_script = dirname(__FILE__) . "/" .
		dirname($this->_script) . "/index.php";

	    if(!file_exists($index_script))
		throw new ViewException(ViewException::NO_SCRIPT, "$controller::$action");
	    else
		$this->_script = "$controller/index.php";
	}

	$this->_base = Registry::get("baseurl");	
    }

    /**
     * Overridable pre-render method
     */
    public function preRender()
    {
	return;
    }

    /**
     * Overridable post-render method
     */
    public function postRender()
    {
	return;
    }

    /**
     * Render this view
     */
    public function render()
    {
	$this->preRender();
	include_once($this->_script);
	$this->postRender();
    }
}
